package com.husd.framework.apidoc;

import com.husd.framework.autocode.dto.ClassDefine;
import com.husd.framework.autocode.dto.ClassFieldDefine;
import com.husd.framework.ddl.AutoCodeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shengdonghu@126.com
 * @date 2023/4/24 10:47
 */
public class JsonClassFileConvert {

    private final String sep = "\n";

    public String generateFileContent(ClassDefine root,
                                      Map<String, ClassDefine> map,
                                      String packageName,
                                      List<String> importList) {

        StringBuilder sb = new StringBuilder();

        if (StringUtils.isNotBlank(packageName)) {
            sb.append(packageName);
            sb.append(sep);
        }

        if (CollectionUtils.isNotEmpty(importList)) {
            for (String s : importList) {
                sb.append(s).append(sep);
            }
        }

        // 先写注解
        List<String> annotationList = root.getAnnotationList();
        if (CollectionUtils.isNotEmpty(annotationList)) {
            for (String annotation : annotationList) {
                append(sb, annotation);
            }
        }


        // 类 public abstract class OpenSdkEbayXmlPageRes extends OpenSdkEbayXmlBaseRes {
        String t1 = "public static class %s extends %s {";
        String t2 = "public static class %s {";
        boolean hasParent = StringUtils.isNoneBlank(root.getParentClassName());
        append(sb, hasParent ?
                String.format(t1, root.getClassName(), root.getParentClassName()) :
                String.format(t2, root.getClassName()));

        // 写每一个属性 private Integer totalPage;
        List<ClassFieldDefine> fieldList = root.getFieldList();
        String t3 = " private %s %s;";
        for (ClassFieldDefine classFieldDefine : fieldList) {

            appendComment(sb, classFieldDefine.getComment());
            append(sb, classFieldDefine.getFieldAnnotation());
            append(sb, String.format(t3, classFieldDefine.getFieldType(), classFieldDefine.getFieldName()));
            // 复合类型和基本类型的处理不同
            boolean complexType = classFieldDefine.isComplex();
            if (complexType) {
                // 复合类型要把该类型对应的类的声明写在下面 fieldType有可能是一个List
                // 导致map匹配不到 所以需要解析出实际的类型
                String classTypeName = classFieldDefine.getClassTypeName();
                if (!AutoCodeUtil.isBaseType(classTypeName)) {
                    ClassDefine subroot = map.get(classTypeName);
                    if (subroot == null) {
                        throw new RuntimeException("根据class name:" + classTypeName + " 没有找到类的定义");
                    }
                    append(sb, generateFileContent(subroot, map, null, null));
                }
            } else {
            }
        }

        append(sb, "}");

        return sb.toString();
    }

    private void appendComment(StringBuilder sb, String comment) {

        if (StringUtils.isNotBlank(comment)) {
            StringBuilder c = new StringBuilder();
            c.append("/**\n");
            c.append(" * ").append(comment).append("\n");
            c.append("**/\n");
            append(sb, c.toString());
        }
    }

    public Map<String, ClassDefine> convert2Map(List<ClassDefine> classDefinesList) {

        Map<String, ClassDefine> map = new HashMap<>();
        for (ClassDefine classDefine : classDefinesList) {

            String key = classDefine.getClassName();
            System.out.println("class name list: " + key);
            map.put(key, classDefine);
        }
        return map;
    }

    private void append(StringBuilder sb, String str) {

        if (StringUtils.isNotBlank(str)) {
            sb.append(str).append(sep);
        }
    }

    public String geneMapEnum(List<String> allPath, String mapEnumName, String packageName) {

        StringBuilder sb = new StringBuilder();
        sb.append(packageName);
        sb.append("import lombok.Getter;\n");
        sb.append("public enum " + mapEnumName + " {\n");

        //String t = "tax_id(\"tax_id\", \"attrList\", \"tax:id\", \"\", \"\"), \n";
        String t = "%s(\"%s\", \"attrList\", \"%s\", \"\", \"\"), \n";
        for (String path : allPath) {

            String name = path.replaceAll(":", "_");
            name = name.replaceAll("-", "_");
            sb.append(String.format(t, name, path, path));
        }

        sb.append(";\n");

        String ttt = "    private String platformPath;\n" +
                "    private String publishInfoField;\n" +
                "    private String key;\n" +
                "    private String field;\n" +
                "    private String remark;\n" +
                "    private boolean required;\n" +
                "\n" +
                "    %s (String platformPath, String publishInfoField, String key, String field, String remark) {\n" +
                "        this.platformPath = platformPath;\n" +
                "        this.publishInfoField = publishInfoField;\n" +
                "        this.key = key;\n" +
                "        this.field = field;\n" +
                "        this.remark = remark;\n" +
                "        this.required = false;\n" +
                "    }\n" +
                "\n" +
                "    %s (String platformPath, String publishInfoField, String key, String field, String remark, boolean required) {\n" +
                "        this.platformPath = platformPath;\n" +
                "        this.publishInfoField = publishInfoField;\n" +
                "        this.key = key;\n" +
                "        this.field = field;\n" +
                "        this.remark = remark;\n" +
                "        this.required = required;\n" +
                "    }\n";
        sb.append(String.format(ttt, mapEnumName, mapEnumName));
        sb.append("}\n");
        return sb.toString();
    }
}
